package cn.neusoft.service.impl;

import cn.neusoft.bean.Business;
import cn.neusoft.bean.Orders;
import cn.neusoft.dao.IOrdersDao;
import cn.neusoft.dao.impl.OrdersDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.OrdersQuery;
import cn.neusoft.service.IOrdersService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;

import java.util.List;

public class OrdersService implements IOrdersService {
    private IOrdersDao ordersDao;

    public OrdersService() {
        ordersDao = new OrdersDao();
    }

    @Override
    public ResultBean add(Orders orders) throws Exception {
       if (orders==null||
               StringUtils.isEmpty(orders.getUserId())||
               orders.getBusinessId()==null||
               StringUtils.isEmpty(orders.getOrderDate())||
               orders.getOrderTotal()==null||
               orders.getDaId()==null||
               orders.getOrderDate()==null){
           return ResultBean.ERROR(10001,"params null");
       }
       int i = ordersDao.add(orders);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add orders fail");
    }

    @Override
    public ResultBean update(Orders orders) throws Exception {
        if (orders == null ||
                orders.getOrderId()==null||
                StringUtils.isEmpty(orders.getUserId())||
                orders.getBusinessId()==null||
                StringUtils.isEmpty(orders.getOrderDate())||
                orders.getOrderTotal()==null||
                orders.getDaId()==null||
                orders.getOrderDate()==null){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = ordersDao.update(orders);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update orders fail");
    }

    @Override
    public ResultBean delete(Integer id) throws Exception {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = ordersDao.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("delete orders fail");
    }

    @Override
    public ResultBean getById(Integer id) throws Exception {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        Orders byId = ordersDao.getById(id);
        return byId!=null?ResultBean.SUCCESS(byId):ResultBean.ERROR("get Orders fail");
    }

    @Override
    public ResultBean getAll() throws Exception {
        List<Orders> ordersList = ordersDao.getAll();
        return ordersList!=null?ResultBean.SUCCESS(ordersList):ResultBean.ERROR("get all orders fail");
    }

    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
        if (baseQuery == null){
            baseQuery = new OrdersQuery();
        }
        PageBean<Orders> pageBean = new PageBean<>();
        //查询总记录数
        int count = ordersDao.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        if (count%baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else {
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }

        //查询集合
        List<Orders> ordersList = ordersDao.getByPage(baseQuery);
        pageBean.setData(ordersList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Override
    public ResultBean batchDel(List<Integer> ids) throws Exception {
        if (ids == null || ids.size()==0){
            return ResultBean.ERROR("params null");
        }

        for (Integer id:ids){
            ordersDao.delete(id);
        }
        return ResultBean.SUCCESS();
    }
}
